package ar.fi.uba.fallas2.business;

import java.sql.PreparedStatement;
import ar.fi.uba.fallas2.data.*;

public class GuessObject extends BusinessObject {
	private String name;
	private static final String insertStatementString = "INSERT INTO object(name) VALUES (?)";
	private static final String updateStatementString = "UPDATE object SET name = ? WHERE id_object = ?";
	private static final String deleteStatementString = "DELETE FROM object WHERE id_object = ?";
	
	public GuessObject() {
		super("object");
	}
	
	public GuessObject(int id) {
		super("object", id);
	}
	
	public boolean equals(Object o) {
		GuessObject other = (GuessObject) o;
		return this.getName().equals(other.getName());
	}
	
	public int hashCode() {
		return 0;
	}
	
	protected void insert() throws DataException {
		PreparedStatement insertStatement = null;
		try {
			insertStatement = Database.getInstance().prepareStatement(insertStatementString);
			insertStatement.setString(1, this.getName());
			insertStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	protected void update() throws DataException {
		PreparedStatement updateStatement = null;
		try {
			updateStatement = Database.getInstance().prepareStatement(updateStatementString);
			updateStatement.setString(1, this.getName());
			updateStatement.setInt(2, this.getId());
			updateStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public void delete() throws DataException {
		PreparedStatement updateStatement = null;
		try {
			updateStatement = Database.getInstance().prepareStatement(deleteStatementString);
			updateStatement.setInt(1, this.getId());
			updateStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}
